package com.woniuxy.oa.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.oa.dao.RoleDAO;
import com.woniuxy.oa.dao.RolePermDAO;
import com.woniuxy.oa.entity.Permission;
import com.woniuxy.oa.entity.Role;
import com.woniuxy.oa.service.RoleService;
import com.woniuxy.oa.util.OaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDAO roleDAO;
    private RolePermDAO rolePermDAO;

    @Override
    public void add(Role role) {
        String roleid = OaUtil.getId();
        role.setId(roleid);
        role.setStatus("1");
        roleDAO.add(role);
    }

    @Override
    public void delById(String[] ids) {
        if (ids != null) {
            for (String id : ids) {
                roleDAO.delById("2", id);
            }
        }
    }

    @Override
    public void update(Role role) {
        roleDAO.update(role);
    }

    @Override
    public List<Role> getRoles() {
        return roleDAO.getRoles();
    }

    @Override
    public Role getById(String id) {
        return roleDAO.getById(id);
    }

    @Override
    public PageInfo getPageBean(Role role, Integer page) {
        PageHelper.startPage(page, 5);
        List<Role> roleList = roleDAO.getAll(role);
        PageInfo pageInfo = new PageInfo(roleList);
        return pageInfo;
    }

    @Override
    public List<Permission> getPerm(String id) {
        return roleDAO.getPerm(id);
    }
}
